/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.client;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.uhilger.netzpult.shared.Folder;

/**
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class FolderSelector extends DialogBox implements ClickHandler {
	
	private int result;
	public static final int RESULT_OK = 1;
	public static final int RESULT_CANCEL = 2;
	
	//private TreeItem selectedTreeItem;
	private BoundTree tree;
	private Button okBtn;
	private Button cancelBtn;
	
	public FolderSelector(OwnerServiceAsync service) {
		this.setHTML("Ordner w&auml;hlen");
		
		tree = new BoundTree(false);
		tree.setService(service);
		//tree.addSelectionHandler(this);
		//tree.addOpenHandler(this);
		//tree.setSize("100%", "100%");

		getRootFolders();

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel.setSpacing(2);
		
		cancelBtn = new Button("Abbrechen", this);
		okBtn = new Button("Speichern", this);

		horizontalPanel.add(cancelBtn);
		horizontalPanel.add(okBtn);

		ScrollPanel scrollPanel = new ScrollPanel();
		scrollPanel.setWidget(tree);
		scrollPanel.setSize("300px", "200px");

		VerticalPanel verticalPanel = new VerticalPanel();
		//verticalPanel.setSize("300px", "200px");		
		verticalPanel.add(scrollPanel);
		verticalPanel.add(horizontalPanel);
		
		setWidget(verticalPanel);
	}
	
	private void getRootFolders() {
		//Folder folder = Folder.getRootFolder();
		BoundTreeServiceAsync service = tree.getService();
		//service.getChildren(folder, new AsyncCallback<List<Folder>>() {
		service.getRootFolders(null, new AsyncCallback<List<Folder>>() {
			public void onFailure(Throwable caught) {
				Window.alert("FolderSelector.getRootFolders error " + caught.getMessage());
			}
			public void onSuccess(List<Folder> result) {
				Iterator<Folder> i = result.iterator();
				while(i.hasNext()) {
					Folder folder = i.next();
					TreeItem newItem = new TreeItem();
					newItem.setHTML(folder.getName());
					newItem.setUserObject(folder);
					newItem.addItem("");
					tree.addItem(newItem);
				}
			}
		});
	}
	

		
	/*private void getChildren(TreeItem item) {
		tree.setSelectedItem(item);
		final TreeItem selectedTreeItem = item;
		//Window.alert("getChildren Item " + ((Folder) item.getUserObject()).getName()); 
		service.getChildren((Folder) item.getUserObject(), new AsyncCallback<List<Folder>>() {
			public void onFailure(Throwable caught) {
				Window.alert("FolderSelector.getChildren error: " + caught.getMessage());
			}
			public void onSuccess(List<Folder> result) {
				Iterator<Folder> i = result.iterator();
				while(i.hasNext()) {
					Folder folder = i.next();
					TreeItem newItem = new TreeItem();
					newItem.setHTML(folder.getName());
					newItem.setUserObject(folder);
					newItem.addItem("");
					selectedTreeItem.addItem(newItem);
				}
			}
		});
	}*/


	public Folder getSelectedFolder() {
		Folder folder = null;
		TreeItem selectedTreeItem = tree.getSelectedItem();
		if(selectedTreeItem != null) {
			folder = (Folder) selectedTreeItem.getUserObject();
		}
		return folder;
	}
	
	public int getResult() {
		return result;
	}

	@Override
	public void onClick(ClickEvent event) {
		Object src = event.getSource();
		if(src.equals(okBtn)) {
			result = RESULT_OK;
		} else if(src.equals(cancelBtn)) {
			result = RESULT_CANCEL;
		}
		hide();
	}

	
	/*private class RootFolderCallback implements AsyncCallback <List<Folder>> {
		public void onFailure(Throwable caught) {
			Window.alert("FolderSelector.getSelectionTree getRootFolders error: " + caught.getMessage());
		}
		public void onSuccess(List<Folder> result) {
			Iterator<Folder> i = result.iterator();
			while(i.hasNext()) {
				Folder folder = i.next();
				TreeItem item = new TreeItem();
				item.setText(folder.getName());
				item.setUserObject(folder);
				item.addItem("");
				tree.addItem(item);
			}
		}
	}*/
	
	/*@Override
	public void onOpen(OpenEvent<TreeItem> event) {
			TreeItem item = event.getTarget();
			//Window.alert("Open Tree Item " + ((Folder) item.getUserObject()).getName()); 
			if (item.getChildCount() <= 1) {
				item.setState(false, false);
				getChildren(item);
				item.setState(true, false);
			}
	}*/


}
